package com.weasel.im.common.codec;

import com.weasel.helper.JsonHelper;
import com.weasel.im.common.domain.Message;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.List;

/**
 * Created by Dylan on 2015/12/4.
 */
public class WebsocketMessageEncoder extends MessageToMessageEncoder<Message> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Message message, List<Object> list) throws Exception {
        TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame(JsonHelper.toJsonString(message));
        list.add(textWebSocketFrame.retain());
    }
}
